#tag Class
Protected Class TileBag
	#tag Method, Flags = &h0
		Sub Constructor(tileArray() as board . Tile)
		  tiles = tileArray
		  tiles.Shuffle
		  tiles.Shuffle 'just a second time as the shuffle sometimes creates strange shuffle-results (too many tiles of the same type in a sequence)
		  tileCount = tiles.Ubound
		  
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Function GetNextTile() As board.Tile
		  if ubound(tiles) <> -1 then
		    tileCount = ubound(tiles) + 1
		    return tiles.Pop
		  end if
		  return nil
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function GetRemainingTiles() As board.Tile()
		  return me.tiles
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub ReturnTileToBag(tile As Tile)
		  'this method is called in the case that the
		  'current drawn tile cannot be placed on the board
		  'so we return it to the bag and reshuffle the bag
		  
		  tiles.Append tile
		  tiles.Shuffle
		  tileCount = tiles.Ubound
		  
		  
		  
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Function IsEmpty() As Boolean
		  'check if there only exists one tile in the bag
		  
		  if ubound(Tiles) = -1 then
		    return true
		  else
		    return false
		  end if
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function GetNumberOfTilesLeft() As Integer
		  return Ubound(tiles) + 1
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub Constructor(tileArray() as board . Tile, shuffle as boolean)
		  tiles = tileArray
		  if shuffle = true then
		    tiles.Shuffle
		    tiles.Shuffle 'just a second time as the shuffle sometimes creates strange shuffle-results (too many tiles of the same type in a sequence)
		  end if
		  tileCount = tiles.Ubound
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Function GetTileByUid(uId as string) As board.Tile
		  dim tile as Tile
		  dim i as Integer
		  
		  for i = 0 to ubound(tiles)
		    if str(tiles(i).GetUid) = str(uId) then
		      tile = tiles(i)
		      exit for
		    end if
		  next i
		  '
		  '#if DebugBuild
		  'if tile = nil then
		  'MsgBox "Tilebag"
		  'end if
		  '#endif
		  
		  return tile
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function SelfCheck() As boolean
		  //only for test-purposes - it seems that the IDE has a bug
		  //sometimes the tilearray has a nil-object
		  #if DebugBuild
		    dim i as integer
		    
		    
		    
		    for i = 0 to ubound(tiles)
		      if tiles (i) = nil then
		        'msgbox "selfcheck-error" // << set stop-point here
		        return true
		      end if
		    next
		    
		    return false
		    
		  #endif
		  
		  
		  
		  
		  
		  
		End Function
	#tag EndMethod


	#tag Property, Flags = &h21
		Private tiles() As board.Tile
	#tag EndProperty

	#tag Property, Flags = &h21
		Private tileCount As Integer
	#tag EndProperty


	#tag ViewBehavior
		#tag ViewProperty
			Name="Name"
			Visible=true
			Group="ID"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Index"
			Visible=true
			Group="ID"
			InitialValue="-2147483648"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Super"
			Visible=true
			Group="ID"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Left"
			Visible=true
			Group="Position"
			InitialValue="0"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Top"
			Visible=true
			Group="Position"
			InitialValue="0"
			InheritedFrom="Object"
		#tag EndViewProperty
	#tag EndViewBehavior
End Class
#tag EndClass
